#include "VSSwitchNode.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSSwitchNode,VSNode)
static bool gs_bStreamRegistered_VSSwitchNode= VSSwitchNode::RegisterMainFactory();
bool VSSwitchNode::ms_bRegisterMainFactory = false;
bool VSSwitchNode::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 
		VSMain::AddInitialFuntion(VSSwitchNode::InitialClassFactory); 
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
bool VSSwitchNode::InitialClassFactory()
{
	ms_ClassFactory.AddElement(ms_Type.GetName(),FactoryFunc);

	return 1;
}
VSObject * VSSwitchNode::FactoryFunc()
{
	return VS_NEW VSSwitchNode;

}
VSSwitchNode::VSSwitchNode()
{
	m_uiActiveNode = 0;
}
VSSwitchNode::~VSSwitchNode()
{

}
bool VSSwitchNode::SetLodDesirePercent(VSREAL fDesirePercent)
{
	if (!VSSpatial::SetLodDesirePercent(fDesirePercent))
	{
		return false;
	}
	if (m_pChild.GetNum() > 0)
	{
		m_uiActiveNode = (unsigned int)((m_pChild.GetNum() - 1) * (1 - fDesirePercent));
	}
	return true;
}
void VSSwitchNode::CreateClodMesh()
{
	return;
}
void VSSwitchNode::ComputeNodeVisibleSet(VSCuller & Culler,bool bNoCull,double dAppTime)
{

	UpDateView(Culler,dAppTime);
	if (m_uiActiveNode < m_pChild.GetNum())
	{
		if(m_pChild[m_uiActiveNode])
		{
			m_pChild[m_uiActiveNode]->ComputeVisibleSet(Culler,bNoCull,dAppTime);

		}
	}
}
void VSSwitchNode::UpdateWorldBound(double dAppTime)
{
	if (m_uiActiveNode < m_pChild.GetNum())
	{
		if(m_pChild[m_uiActiveNode])
			m_WorldBV = m_pChild[m_uiActiveNode]->m_WorldBV; 
	}
}
void VSSwitchNode::UpdateLightState(double dAppTime)
{
	UpdateLightAll(dAppTime);
	if(m_pAllLight.GetNum() > 0)
		m_pAllLight.Clear();
	if(m_pLight.GetNum() > 0)
		m_pAllLight.AddElement(m_pLight,0,m_pLight.GetNum() - 1);

	if (m_uiActiveNode < m_pChild.GetNum())
	{
		if(m_pChild[m_uiActiveNode])
		{
			if(m_pChild[m_uiActiveNode]->m_pAllLight.GetNum() > 0)
				m_pAllLight.AddElement(m_pChild[m_uiActiveNode]->m_pAllLight,0,m_pChild[m_uiActiveNode]->m_pAllLight.GetNum() - 1);
		}
	}


}
void VSSwitchNode::UpdateCameraState(double dAppTime)
{
	UpdateCameraAll(dAppTime);
	if(m_pAllCamera.GetNum() > 0)
		m_pAllCamera.Clear();
	if(m_pCamera.GetNum() > 0)
		m_pAllCamera.AddElement(m_pCamera,0,m_pCamera.GetNum() - 1);

	if (m_uiActiveNode < m_pChild.GetNum())
	{
		if(m_pChild[m_uiActiveNode])
		{
			if(m_pChild[m_uiActiveNode]->m_pAllCamera.GetNum() > 0)
				m_pAllCamera.AddElement(m_pChild[m_uiActiveNode]->m_pAllCamera,0,m_pChild[m_uiActiveNode]->m_pAllCamera.GetNum() - 1);
		}
	}


}

VSSpatial * VSSwitchNode::GetActiveNode()const
{
	if (m_uiActiveNode < m_pChild.GetNum())
	{
		return m_pChild[m_uiActiveNode];
	}
	return NULL;
}

